/*
 * vsock.h
 *
 *  Created on: Apr 24, 2011
 *      Author: xrose
 */

#ifndef _SOCKET_H_
#define _SOCKET_H_

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string>
#include <iostream>

#include "Util/Exception.h"

#define MAX_STR_LEN 512
#define BUFFER_SIZE 10000

typedef int socket_t;
const int INVALID_SOCKET = -1;
#define SOCKET_ERROR -1

using namespace std;
class Socket {
public:
	Socket();
	virtual ~Socket();
	/**
	 * create new socket
	 */
	bool create();
	/**
	 * bind socket to port
	 */
	bool bind(const int port);
	/**
	 * close the socket
	 */
	bool close();
	/**
	 * connect to host & port
	 */
	bool connect(const string host, const int port);
	/**
	 * send data to host
	 */
	bool send(const string data) const; //place const here to prevent method send alter any :D
	/**
	 * receive data from host
	 */
	int receive(char* buffer) const;
	/**
	 * isValid: check if a socket
	 */
	int getSock(){
		return sd;
	}
	void setSock(int ssd){
		sd = ssd;
	}
	bool isValid(){return sd!=-1;};
private:
	int sd;		//socket description
	sockaddr_in serverAddr;	//server address
	sockaddr_in localAddr;	//local address
};

#endif /* _SOCKET_H_ */
